home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xsok-1.000 / xsok-1 / xsok-1.01 / src / xfopen.c < prev    next >
C/C++ Source or Header  |  1994-11-24  |  2KB  |  87 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    Xsok version 1.00 -- module xfopen.c                     */
  5. /*                                         */
  6. /*    Possible emulation of the popen() / pclose() functions.             */
  7. /*    Written by Michael Bischoff (mbi@mo.math.nat.tu-bs.de)             */
  8. /*    November-1994                                 */
  9. /*    see COPYRIGHT.xsok for Copyright details                 */
  10. /*                                         */
  11. /*                                         */
  12. /*****************************************************************************/
  13. #ifndef _POSIX_SOURCE
  14. #define _POSIX_SOURCE
  15. #endif
  16.  
  17. #ifdef HAVE_POPEN    /* extra prototype (they are not POSIX.1) */
  18.  
  19. #include "xsok.h"
  20.  
  21. FILE *popen(const char *, const char *);
  22. int pclose(FILE *);
  23.  
  24. FILE *zreadopen(const char *filename) {
  25.     char zcmd[MAXXSOKDIRLEN+20+100];    /* assume strlen(GUNZIP_PATH) <= 100 */
  26.     sprintf(zcmd, "%s < %s.gz", GUNZIP_PATH, filename);
  27.     return popen(zcmd, "r");
  28. }
  29. void zreadclose(FILE *fp) {
  30.     pclose(fp);
  31. }
  32.  
  33. #else
  34.  
  35. #include <sys/types.h>
  36. #include <sys/wait.h>
  37. #include <unistd.h>
  38. #include "xsok.h"
  39.  
  40. #ifndef GUNZIP_PATH
  41. #define GUNZIP_PATH "gunzip"
  42. #endif
  43.  
  44. FILE *zreadopen(const char *filename) {
  45.     int filedes[2];
  46.     char zname[MAXXSOKDIRLEN+17];
  47.     FILE *fp;
  48.  
  49.     sprintf(zname, "%s.gz", filename);
  50.     if (access(zname, R_OK))
  51.     return NULL;        /* simpler and more correct */
  52.     if (GUNZIP_PATH[0] == '/')
  53.     if (access(GUNZIP_PATH, X_OK))
  54.         return NULL;        /* simpler and more correct */
  55.     /* following taken from D. Lewine "POSIX programmers guide, page 104 */
  56.     /* try fork and exec. This requires the path of the gzip binary */
  57.     /* but it is much faster */
  58.     if (pipe(filedes))
  59.     return NULL;    /* cannot create pipe */
  60.     switch (fork()) {
  61.     case -1:        /* cannot fork */
  62.     /* close the pipe and return NULL */
  63.     close(filedes[0]);
  64.     close(filedes[1]);
  65.     return NULL;
  66.     case 0:            /* we are the child. exec the gunzip binary */
  67.     close(STDOUT_FILENO);
  68.     dup(filedes[1]);
  69.     close(filedes[0]);
  70.     close(filedes[1]);
  71.     execlp(GUNZIP_PATH, "gunzip", "-c", zname, NULL);
  72.     exit(1);    /* if exec failed! */
  73.     default:        /* we are the main process */
  74.     fp = fdopen(filedes[0], "r");
  75.     close(filedes[1]);
  76.     }
  77.     return fp;
  78. }
  79.  
  80. void zreadclose(FILE *fp) {
  81.     int status;
  82.     fclose(fp);
  83.     wait(&status);
  84. }
  85.  
  86. #endif
  87.